import 'package:bruno/bruno.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:lazy_food/components/keepAliveWrapper/keep_alive_arapper.dart';

import '../components/calendar/calendar.dart';

class MinePage extends StatefulWidget {
  @override
  State<MinePage> createState() => _MinePageState();
}

class _MinePageState extends State<MinePage>
    with AutomaticKeepAliveClientMixin {
  String _title = "";
  ScrollController _scrollController = ScrollController();
  List<String> _months = [
    '一月',
    '二月',
    '三月',
    '四月',
    '五月',
    '六月',
    '七月',
    '八月',
    '九月',
    '十月',
    '十一月',
    '十二月'
  ];
  DateTime _currentDate = DateTime.now();

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.offset > 44) {
        setState(() {
          _title = "黄焖鸡_vn6Exo";
        });
      } else {
        setState(() {
          _title = "";
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Scaffold(
      backgroundColor: Colors.black,
      appBar: AppBar(
        title: Text(_title),
        backgroundColor: Colors.black,
        leading: IconButton(
          onPressed: () {
            print("设置");
          },
          icon: Icon(
            Icons.settings,
            color: Colors.white,
          ),
        ),
        actions: [
          IconButton(
            onPressed: () {
              print("相机");
            },
            icon: Icon(
              Icons.camera,
              color: Colors.white,
            ),
          )
        ],
      ),
      body: KeepAliveWrapper(
        child: ListView.builder(
          itemCount: 10,
          controller: _scrollController,
          itemBuilder: (context, index) {
            if (index == 0) {
              return Container(
                padding: EdgeInsets.only(left: 20, right: 20, bottom: 15),
                child: Row(
                  children: [
                    SizedBox(
                      height: 15,
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          '黄焖鸡_vn6Exo',
                          style: TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        Text(
                          'IP属地: 北京',
                          style: TextStyle(color: Colors.white54, fontSize: 12),
                        ),
                      ],
                    ),
                    Spacer(),
                    ClipRRect(
                      borderRadius: BorderRadius.circular(37.5),
                      child: CachedNetworkImage(
                        imageUrl:
                            'https://c-ssl.dtstatic.com/uploads/blog/202201/23/20220123222213_2899a.thumb.1000_0.jpeg',
                        placeholder: (context, url) {
                          return Image.asset('assets/images/avatar.png');
                        },
                        errorWidget: (context, url, error) {
                          return Image.asset('assets/images/avatar.png');
                        },
                        width: 75,
                        height: 75,
                      ),
                    ),
                    SizedBox(
                      height: 15,
                    ),
                  ],
                ),
              );
            } else if (index == 1) {
              return Container(
                height: 60,
                child: Column(
                  children: [
                    Divider(
                      height: 1,
                      color: Colors.white12,
                    ),
                    SizedBox(height: 10),
                    Expanded(
                      child: GridView.builder(
                        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                            crossAxisCount: 4),
                        itemCount: 4,
                        physics: NeverScrollableScrollPhysics(),
                        itemBuilder: (context, index) {
                          return Column(
                            children: [
                              Text('1',
                                  style: TextStyle(
                                      color: Colors.white, fontSize: 14)),
                              Text(
                                '笔记',
                                style: TextStyle(
                                    color: Colors.white24, fontSize: 14),
                              ),
                            ],
                          );
                        },
                      ),
                    ),
                  ],
                ),
              );
            } else if (index == 2) {
              return Row(
                children: [
                  SizedBox(
                    width: 20,
                  ),
                  Container(
                    height: 300,
                    width: MediaQuery.of(context).size.width - 40,
                    child: CalendarView(
                      // 自定义header
                      buildHeader: (date) {
                        return Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          children: [
                            SizedBox(width: 15),
                            Expanded(
                              child: Text(
                                '${_months[date.month - 1]} ${date.year}',
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 16,
                                ),
                              ),
                            ),
                            Spacer(),
                            IconButton(
                              icon: Icon(Icons.more_horiz,
                                  size: 15, color: Colors.white),
                              onPressed: _onMore,
                            ),
                          ],
                        );
                      },
                      onClickDay: (day) {
                        print(day.dateTime.day);
                      },
                    ),
                    decoration: BoxDecoration(
                      color: Colors.white38,
                      borderRadius: BorderRadius.circular(20),
                    ),
                  ),
                ],
              );
            } else {
              return Column(
                children: [],
              );
            }
          },
        ),
      ),
    );
  }

  void _onMore() {
    EasyLoading.showInfo('点击更多');
  }
}
